package com.shaozi.nc.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shaozi.data.mq.business.base.SourceTypeConsts;
import com.shaozi.data.mq.business.base.SourceTypeConsts.OA;
import com.shaozi.data.mq.business.bean.MqTargetUserBean;
import com.shaozi.data.mq.business.utils.MqUtil;
import com.shaozi.nc.bean.base.BaseSecretaryMsgBean;
import com.shaozi.nc.bean.resp.SecretaryMsgBean;
import com.shaozi.nc.data.model.SecretaryModel;
import com.shaozi.nc.utils.spel.SecretarySpel;
import com.shaozi.utils.common.BeanUtils;
import com.shaozi.utils.spel.SimpleEval;

public class SecretaryHelper {

	private static List<Integer> silentSourceTypes = Arrays.asList();

	private SecretaryHelper() {

	}

	/**
	 * 小秘书通知类型
	 * 
	 * @param sourceType
	 * @param userType
	 * @return
	 */
	public static int getNotifyTypeFromSourceType(int sourceType, int userType) {
		int baseSourceType = MqUtil.getBaseSourceType(sourceType);
		if (SourceTypeConsts.Data.BriefReport.Base == baseSourceType) {
			return SecretaryModel.NotifyType_BriefReport;
		} else if (SourceTypeConsts.Operation.Advice.Base == baseSourceType) {
			return SecretaryModel.NotifyType_Operation;
		}
		return SecretaryModel.NotifyType_Remind;
	}

	/**
	 * 将oa.base，拆开
	 * 
	 * @param oldTypes
	 * @return
	 */
	public static String[] transType(String[] oldTypes) {
		if (oldTypes == null)
			return null;
		Map<String, Integer> undup = new HashMap<String, Integer>();
		for (String oldType : oldTypes) {
			if (String.valueOf(OA.Base).equals(oldType)) {
				undup.put(String.valueOf(OA.Report.Base), 1);
				undup.put(String.valueOf(OA.Task.Base), 1);
				undup.put(String.valueOf(OA.Approve.Base), 1);
				undup.put(String.valueOf(OA.Attendance.Base), 1);
				undup.put(String.valueOf(OA.Crm_Sale.Base), 1);
				undup.put(String.valueOf(OA.Crm_Custom.Base), 1);

			} else {
				undup.put(oldType, 1);
			}
		}
		return undup.keySet().toArray(new String[] {});
	}

	public static boolean isSilentSourceType(int sourceType) {
		return silentSourceTypes.indexOf(Integer.valueOf(sourceType)) >= 0;
	}

	public static List<Integer> getSlientSourceTypes() {
		return silentSourceTypes;
	}

	public static SecretaryMsgBean secretaryModelToMsgBean(SecretaryModel secretary, String userId) {
		if ((secretary.getUsers() == null) || (secretary.getUsers().isEmpty()))
			return null;
		for (MqTargetUserBean user : secretary.getUsers()) {
			if (user.getUserId() != null && user.getUserId().equals(userId)) {
				SecretaryMsgBean msgBean = new SecretaryMsgBean();
				initSecretaryMsgBean(msgBean, secretary, user);
				return msgBean;
			}
		}
		return null;
	}

	public static List<SecretaryMsgBean> secretaryModelToMsgBean(SecretaryModel secretary) {
		if ((secretary.getUsers() == null) || (secretary.getUsers().isEmpty()))
			return null;
		List<SecretaryMsgBean> ret = new ArrayList<>();
		for (MqTargetUserBean user : secretary.getUsers()) {

			SecretaryMsgBean msgBean = new SecretaryMsgBean();
			initSecretaryMsgBean(msgBean, secretary, user);
			ret.add(msgBean);
		}
		return ret;
	}

	/**
	 * 1未开始 2完成 3进行中 4暂停 5取消 6延期
	 * 
	 * @param taskStatus
	 * @return
	 */
	public static String getTaskStatus(int taskStatus) {
		switch (taskStatus) {
		case 1:
			return "未完成";
		case 2:
			return "完成";
		case 3:
			return "进行中";
		case 4:
			return "暂停";
		case 5:
			return "取消";
		case 6:
			return "延期";
		default:
			break;
		}
		return "";
	}

	public static String getCrmDupCheckingActionType(Integer actionType) {
		if (actionType == null)
			return "开启";
		switch (actionType) {
		case 1:
			return "开启";
		case 2:
			return "关闭";
		default:
			break;
		}
		return "";
	}

	public static String toUserNameDef() {
		return BeanUtils.getConfig().getValue("push.msg.tousername.def");
	}

	public static String hisUserNameDef() {
		return BeanUtils.getConfig().getValue("push.msg.hisusername.def");
	}

	private static boolean between(int val, int low, int high) {
		return (val >= low) && val < high;
	}

	private static void initSecretaryMsgBean(SecretaryMsgBean msgBean, SecretaryModel secretary,
			MqTargetUserBean user) {
		commonInitSecretaryMsgBean(msgBean, secretary, user);
		msgBean.setSecretaryId(secretary.getId());
	}

	protected static void commonInitSecretaryMsgBean(BaseSecretaryMsgBean msgBean, SecretaryModel secretary,
			MqTargetUserBean user) {
		SimpleEval eval = new SimpleEval();
		SecretarySpel sspel = new SecretarySpel();
		Map<String, Object> params = sspel.getSpelTitleParams(secretary, user.getUserId());
		String title = params == null ? secretary.getTitle() : (String) eval.eval(secretary.getTitle(), params);
		params = sspel.getSpelContentParams(secretary, user.getUserId());
		String content = params == null ? secretary.getContent() : (String) eval.eval(secretary.getContent(), params);

		msgBean.setContent(content);
		msgBean.setTitle(title);
		msgBean.setExtra(secretary.getExtra());
		// msgBean.setFromUserId(secretary.getTriggerUserId());
		msgBean.setFromUserId(secretary.getFromUserId());
		msgBean.setSourceType(secretary.getSourceType());
		msgBean.setToUserId(user.getUserId());
		msgBean.setInsertTime(user.getCreateTime());
		msgBean.setUpdateTime(user.getUpdateTime());
		msgBean.setNotifyType(secretary.getNotifyType());
		msgBean.setModuleType(secretary.getType());
		msgBean.setIsRead(user.getIsRead());
	}
}
